package com.vecspace.data.model;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.*;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import com.vecspace.data.numerical.HMoneyWanYuan;
import com.vecspace.data.numerical.HMoneyYuan;
import com.vecspace.data.numerical.HNumber;
import com.vecspace.data.numerical.HPercent;


public class RowMapper implements Map<String,Object>{
	private Map<String,Object> row;
	public RowMapper(Map<String,Object> row) {
		this.row = row;
	}
	
	/**根据字段名（不区分大小写）获取值，如果字段名不存在，则返回null
	 * @param columnName
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public String getString(String columnName){
		if(!row.containsKey(columnName)) return null;
		Object obj = row.get(columnName);
		if(obj == null) {
			return null;
		}
		return obj.toString();
	}
	
	/**根据字段名（不区分大小写）获取值，如果字段名不存在，则返回null
	 * 
	 * @param columnName
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public Integer getInteger(String columnName){
		if(!row.containsKey(columnName)) return null;
		Object obj = row.get(columnName);
		if(obj == null) {
			return null;
		}
		else if(obj instanceof Integer) {
			return (Integer)obj;
		}
		else if(obj instanceof Long) {
			return ((Long)obj).intValue();
		}
		else if(obj instanceof BigInteger) {
			return ((BigInteger)obj).intValue();
		}
		else if(obj instanceof BigDecimal) {
			return ((BigDecimal)obj).intValue();
		}
		else if(obj instanceof Double) {
			return ((Double)obj).intValue();
		}
		else if(obj instanceof Float) {
			return ((Float)obj).intValue();
		}else return null;
	}
	/**根据字段名（不区分大小写）获取值，如果字段名不存在，则返回null
	 * 如果值为空(null)则返回0
	 * @param columnName
	 * @return
	 * @throws Exception 
	 * @author: wanghua
	 */
	public int getInt(String columnName) {
		Integer num = getInteger(columnName);
		if(num == null) return 0;
		else return num.intValue();
	}
	

	public int get_int(String columnName) {
		Integer num = getInt(columnName);
		if(num == null)return 0;
		return num.intValue();
	}
	
	/**根据字段名（不区分大小写）获取值，如果字段名不存在，则返回null
	 * 
	 * @param columnName
	 * @return
	 * @author: wanghua
	 */
	public Long getLong(String columnName) {
		if(!row.containsKey(columnName)) return null;
		Object obj = row.get(columnName);
		if(obj == null) {
			return null;
		}
		else if(obj instanceof Integer) {
			return ((Integer)obj).longValue();
		}
		else if(obj instanceof Long) {
			return (Long)obj;
		}
		else if(obj instanceof BigInteger) {
			return ((BigInteger)obj).longValue();
		}
		else if(obj instanceof BigDecimal) {
			return ((BigDecimal)obj).longValue();
		}
		else if(obj instanceof Double) {
			return ((Double)obj).longValue();
		}
		else if(obj instanceof Float) {
			return ((Float)obj).longValue();
		}
		else {
			return null;
		}
	}

	public long get_long(String columnName) {
		Long num = getLong(columnName);
		if(num == null)return 0;
		return num.longValue();
	}
	
	/**根据字段名（不区分大小写）获取值，如果字段名不存在，则返回null
	 * 
	 * @param columnName
	 * @return
	 * @author: wanghua
	 */
	public Double getDouble(String columnName){
		if(!row.containsKey(columnName)) return null;
		Object obj = row.get(columnName);
		if(obj == null) {
			return null;
		}
		else if(obj instanceof BigDecimal) {
			return ((BigDecimal)obj).doubleValue();
		}
		else if(obj instanceof Double) {
			return (Double)obj;
		}
		else if(obj instanceof Integer) {
			return ((Integer)obj).doubleValue();
		}
		else if(obj instanceof Long) {
			return ((Long)obj).doubleValue();
		}
		else if(obj instanceof BigInteger) {
			return ((BigInteger)obj).doubleValue();
		}
		else if(obj instanceof Float) {
			return ((Float)obj).doubleValue();
		}
		else {
			return null;
		}
	}
	
	/**
	 * 根据字段名（不区分大小写）获取值，如果字段名不存在，返回null
	 * @param columnName
	 * @return 
	 * @author  wanghua
	 */
	public BigDecimal getBigDecimal(String columnName){
		if(!row.containsKey(columnName)) return null;
		Object obj = row.get(columnName);
		if(obj == null) {
			return null;
		}
		if(obj instanceof BigDecimal) {
			return (BigDecimal)obj;
		}else if(obj instanceof Integer) {
			return BigDecimal.valueOf((Integer)obj);
		}
		else if(obj instanceof Long) {
			return BigDecimal.valueOf((Long)obj);
		}
		else if(obj instanceof BigInteger) {
			return BigDecimal.valueOf(((BigInteger)obj).intValue());
		}
		else if(obj instanceof Double) {
			return BigDecimal.valueOf((Double)obj);
		}
		else if(obj instanceof Float) {
			return BigDecimal.valueOf(((Float)obj).doubleValue());
		}
		else {
			return null;
		}
	}
	/**根据字段名（不区分大小写）获取值，如果字段名不存在，则返回null
	 * 
	 * @param columnName
	 * @return
	 * @author: wanghua
	 */
	public Date getDate(String columnName){
		if(!row.containsKey(columnName)) return null;
		Object obj = row.get(columnName);
		if(obj == null) {
			return null;
		}
		else if(obj instanceof Date) {
			return (Date)obj;
		}
		else if(obj instanceof LocalDate) {
			LocalDate localDate = (LocalDate)obj;
			ZoneId zone = ZoneId.systemDefault();
			Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
			java.util.Date date = Date.from(instant);
			return date;
		}
		else if(obj instanceof LocalDateTime) {
			LocalDateTime localDateTime = (LocalDateTime)obj;
			ZoneId zone = ZoneId.systemDefault();
			Instant instant = localDateTime.atZone(zone).toInstant();
			java.util.Date date = Date.from(instant);
			return date;
		}
		else {
			return null;
		}
	}
	/**根据字段名（不区分大小写）获取值，如果字段名不存在，则返回null
	 * 
	 * @param columnName
	 * @return
	 * @author: wanghua
	 */
	public LocalDateTime getLocalDateTime(String columnName){
		if(!row.containsKey(columnName)) return null;
		Object obj = row.get(columnName);
		if(obj == null) {
			return null;
		}
		else if(obj instanceof Date) {
			Date date = (Date)obj;
			Instant instant = Instant.ofEpochMilli(date.getTime());
			ZoneId zone = ZoneId.systemDefault();
			LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
			return localDateTime;
		}
		else if(obj instanceof LocalDate) {
			LocalDate localDate = (LocalDate)obj;
			LocalDateTime localDateTime = localDate.atTime(LocalTime.MIN);
			return localDateTime;
		}
		else if(obj instanceof LocalDateTime) {
			LocalDateTime localDateTime = (LocalDateTime)obj;
			return localDateTime;
		}
		else {
			return null;
		}
	}
	/**根据字段名（不区分大小写）获取值，如果字段名不存在，则返回null
	 * 
	 * @param columnName
	 * @return
	 * @author: wanghua
	 */
	public LocalDate getLocalDate(String columnName){
		if(!row.containsKey(columnName)) return null;
		Object obj = row.get(columnName);
		if(obj == null) {
			return null;
		}
		else if(obj instanceof Date) {
			Date date = (Date)obj;
			//解决java.lang.UnsupportedOperationException
			Date date1 = new Date(date.getTime());
			Instant instant = date1.toInstant();
			ZoneId zone = ZoneId.systemDefault();
			LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
			LocalDate localDate = localDateTime.toLocalDate();
			return localDate;
		}
		else if(obj instanceof LocalDate) {
			LocalDate localDate = (LocalDate)obj;
			return localDate;
		}
		else if(obj instanceof LocalDateTime) {
			LocalDateTime localDateTime = (LocalDateTime)obj;
			return localDateTime.toLocalDate();
		}
		else {
			return null;
		}
	}

	/**根据字段名（不区分大小写）获取值，如果字段名不存在，则返回null
	 * 
	 * @param columnName
	 * @return
	 * @author: wanghua
	 */
	public Boolean getBoolean(String columnName) {
		if(!row.containsKey(columnName)) return null;
		Object obj = row.get(columnName);
		if(obj == null) {
			return null;
		}
		else if(obj instanceof Integer) {
			return ((Integer)obj).intValue() != 0;
		}
		else if(obj instanceof Short) {
			return ((Short)obj).intValue() != 0;
		}
		else if(obj instanceof String) {
			String val = (String)obj;
			if(val.length() == 0) return null;
			return val.equalsIgnoreCase("Y") || val.equalsIgnoreCase("T")  || val.equalsIgnoreCase("YES")  || val.equalsIgnoreCase("TRUE");
		}
		else if(obj instanceof Long) {
			return (Long)obj != 0;
		}
		else if(obj instanceof BigInteger) {
			return ((BigInteger)obj).longValue() != 0;
		}
		else if(obj instanceof BigDecimal) {
			return ((BigDecimal)obj).longValue() != 0;
		}
		else {
			return null;
		}
	}
	public HNumber getHNumber(String columnName) {
		BigDecimal value = this.getBigDecimal(columnName);
		if(value == null)return null;
		return new HNumber(value);
	}
	
	public HMoneyYuan getMoneyYuan(String columnName) {
		BigDecimal value = this.getBigDecimal(columnName);
		if(value == null)return null;
		return new HMoneyYuan(value);
	}
	
	public HMoneyWanYuan getMoneyWanYuan(String columnName) {
		BigDecimal value = this.getBigDecimal(columnName);
		if(value == null)return null;
		return new HMoneyWanYuan(value);
	}
	
	public HPercent getPercent(String columnName) {
		BigDecimal value = this.getBigDecimal(columnName);
		if(value == null)return null;
		return new HPercent(value);
	}
	@Override
	public int size() {
		return row.size();
	}
	@Override
	public boolean isEmpty() {
		return row.isEmpty();
	}
	@Override
	public boolean containsKey(Object key) {
		return row.containsKey(key);
	}
	@Override
	public boolean containsValue(Object value) {
		return row.containsValue(value);
	}
	@Override
	public Object get(Object key) {
		return row.get(key);
	}
	@Override
	public Object put(String key, Object value) {
		return row.put(key, value);
	}
	@Override
	public Object remove(Object key) {
		return row.remove(key);
	}
	@Override
	public void putAll(Map<? extends String, ? extends Object> m) {
		row.putAll(m);
	}
	@Override
	public void clear() {
		row.clear();
	}
	@Override
	public Set<String> keySet() {
		return row.keySet();
	}
	@Override
	public Collection<Object> values() {
		return row.values();
	}
	@Override
	public Set<Entry<String, Object>> entrySet() {
		return row.entrySet();
	}
	
	
}
